/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.sms.channel.jdcloud;

import com.jdcloud.sdk.auth.CredentialsProvider;
import com.jdcloud.sdk.auth.StaticCredentialsProvider;
import com.jdcloud.sdk.http.HttpRequestConfig;
import com.jdcloud.sdk.http.Protocol;
import com.jdcloud.sdk.service.sms.client.SmsClient;
import com.jdcloud.sdk.service.sms.model.BatchSendRequest;
import com.jdcloud.sdk.service.sms.model.BatchSendResult;
import com.lvyh.lightframe.sms.async.AsyncThreadPoolExecutor;
import com.lvyh.lightframe.sms.common.SmsMultipartContent;
import com.lvyh.lightframe.sms.processor.AbstractSmsPushProcessor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * JD cloud sending and processing
 *
 * @author lvyh 2021/03/13.
 */
@Slf4j
public class JdCloudSmsPushProcessor extends AbstractSmsPushProcessor<JdCloudProperties> {

    private final SmsClient smsClient;
    private AsyncThreadPoolExecutor executor = new AsyncThreadPoolExecutor();

    public JdCloudSmsPushProcessor(JdCloudProperties properties) {
        super(properties);
        CredentialsProvider credentialsProvider = new StaticCredentialsProvider(properties.getAccessKeyId(), properties.getAccessKeySecret());
        smsClient = SmsClient.builder().credentialsProvider(credentialsProvider).httpRequestConfig(new HttpRequestConfig.Builder().protocol(Protocol.HTTP).build()).build();
    }

    @Override
    public boolean sendMsg(SmsMultipartContent smsMultipartContent, List<String> phones) {

        String templateId = smsMultipartContent.getTemplateCode();

        if (templateId == null) {
            log.info("templateId invalid");
            return false;
        }

        List<String> orderedParams = properties.getParamsOrder();

        ArrayList<String> params = new ArrayList<>();

        if (!orderedParams.isEmpty()) {
            Map<String, String> paramMap = smsMultipartContent.getTemplateParams();
            for (String paramName : orderedParams) {
                String paramValue = paramMap.get(paramName);
                params.add(paramValue);
            }
        }

        BatchSendRequest request = new BatchSendRequest();
        request.setRegionId(properties.getRegion());
        request.setTemplateId(templateId);
        request.setSignId(properties.getSignId());
        request.setPhoneList(phones);
        request.setParams(params);
        BatchSendResult result = smsClient.batchSend(request).getResult();
        Boolean status = result.getStatus();
        log.info("send sms result,code:{},message:{}", result.getCode(), result.getMessage());
        return status;
    }

    @Override
    public boolean asyncSendMsg(SmsMultipartContent smsMultipartContent, List<String> phones) {
        executor.submit(() -> asyncSend(smsMultipartContent, phones));
        return true;
    }

    @Override
    public String getChannelName() {
        return "jdCloud";
    }
}
